<template>
  <div class="page">
    <div class="tools-bar">
      <div class="left-wrap">
        <el-input prefix-icon="el-icon-search"  placeholder="请输入搜索关键字" size="small" v-model="searchKey"></el-input>
        <el-button size="small" type="primary" @click="handleSearch">查询</el-button>
        <el-button size="small" type="warning" @click="cleanSearch">重置</el-button>
        <!-- {{moduleId(type)}} -->
        <el-button type="primary" size="small" @click="handleAddApp">新增</el-button>
      </div>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="appName"
        label="应用名称"
        width="auto">
      </el-table-column>
      <el-table-column
        prop="appKey"
        label="appKey"
        width="auto">
      </el-table-column>
      <el-table-column
        prop="userName"
        label="创建人"
        width="auto">
      </el-table-column>
      <el-table-column
        prop="createDate"
        label="更新时间"
        width="auto">
      </el-table-column>
      <el-table-column
        prop="isDelete"
        label="状态"
        width="auto">
        <template slot-scope="scope">
          <el-tag size="small" type="sucess" v-if="scope.row.isDelete == 1">
            禁用
          </el-tag>
          <el-tag size="small" type="info" v-if="scope.row.isDelete == 0">
            启用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="auto">
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-edit" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="success" icon="el-icon-circle-plus" size="small" @click="jumpToServer(scope.row.id)">配置服务器</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination ref="pageInfo" :total="pageInfo.totalElements" :page.sync="pageInfo.currentPage" @pagination="pagination"></pagination>
    <el-dialog title="应该新增或编辑" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="应用名称" prop="appName">
          <el-input v-model="ruleForm.appName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { api } from '@/utils'
  import { pagination } from '@/components'
  export default {
    methods: {
      // 配置服务器按钮点击方法
      jumpToServer (data) {
        this.cleanForm(this.ruleForm)
        this.$router.push({
          path: 'serverList',
          query: {
            appId: data
          }
        })
      },
      cleanForm (data) {
        Object.keys(data).forEach(function (key) {
          data[key] = ''
        })
      },
      // 新增按钮点击方法
      handleAddApp () {
        this.dialogFormVisible = true
        // 清空form中的数据
        this.cleanForm(this.ruleForm)
      },
      // 编辑按钮点击方法
      handleEdit (data) {
        this.ruleForm.appName = data.appName
        this.ruleForm.id = data.id
        this.dialogFormVisible = true
      },
      // 新增应用确定按钮提交 方法
      submit () {
        let url = ''
        if (this.ruleForm.id !== '') {
          url = api.editApp
        } else {
          url = api.addApp
        }
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            this.$axios.request({
              url: url,
              method: 'post',
              params: this.ruleForm
            }).then(data => {
              if (data.success === true) {
                this.$message({
                  message: '保存成功',
                  duration: 2000,
                  type: 'success'
                })
                this.dialogFormVisible = false
                this.getData()
              } else {
                this.$message({
                  message: '保存失败',
                  duration: 2000,
                  type: 'error'
                })
              }
            })
          } else {
            return false
          }
        })
      },
      // 查询方法
      handleSearch () {
        let params = {}
        params.appName = this.searchKey
        this.getData(params)
      },
      // 重置方法
      cleanSearch () {
        this.searchKey = ''
      },
      // 获取数据
      getData (param) {
        this.$axios.get(api.getAppList, {
          params: param
        }).then(data => {
          let result = data.data
          console.log(result)
          this.pageInfo.totalElements = data.total
          if (data.success === true) {
            this.tableData = result
          } else {
            this.$message({
              message: '请求失败',
              duration: 2000,
              type: 'error'
            })
          }
        })
      },
      // 列表分页操作事件
      pagination (e) {
        let params = {}
        params.pageNum = e.page
        params.pageSize = e.limit
        this.getData(params)
      }
    },

    data () {
      return {
        tableData: [],
        pageInfo: {
          totalElements: 0,
          currentPage: 1
        },
        searchKey: '',
        ruleForm: {
          id: '',
          appName: ''
        },
        rules: {
          appName: [
            { required: true, message: '请输入应用名称', trigger: 'blur' }
          ]
        },
        dialogFormVisible: false
      }
    },
    components: {
      pagination
    },
    mounted () {
      this.getData()
    }
  }
</script>
